<?php

/**
 * 商标管理系统 v1.0.1
 *
 * 商业运营到ubtm.cn官网进行授权使用。
 *
 * (c) cighsen02 <xiayu@959602.com>
 *
 * Create at：2025.07.04
*/

namespace App\Http\Controllers\Auth;

use App\Exceptions\JsonResponseException;
use App\Http\Controllers\Controller;
use App\Services\Support\QrCode;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * 二维码生成
 */
class QrCodeController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $url    = $request->query('url', 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net');
        $type   = $request->query('type', 'png');

        try {
            // 数据验证
            $data = new QrCode()->image(
                $url,
                $type
            );
        }
        catch (Exception $e) {

            throw new JsonResponseException($e->getMessage(), 401);
        }

        return $this->sendResponse(
            [
                'base64' => $data
            ], 
            'success'
        );
    }
}